<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="肥料类型" prop="fertilizerTypeId">
        <el-select v-model="queryParams.fertilizerTypeId" placeholder="请选择化肥类型">
          <el-option
            v-for="type in fertilizer_type"
            :key="type.fertilizerTypeId"
            :value="type.fertilizerTypeId"
            :label="type.fertilizerType"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="肥料名称" prop="fertilizerNameId">
        <el-select v-model="queryParams.fertilizerNameId" placeholder="请选择化肥名称">
          <el-option
            v-for="name in fertilizer_name"
            :key="name.fertilizerNameId"
            :value="name.fertilizerNameId"
            :label="name.fertilizerName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="肥料品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入肥料品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购自公司" prop="originCompany">
        <el-input
          v-model="queryParams.originCompany"
          placeholder="请输入购自公司"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.applyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请人" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择申请人" clearable size="small">
          <el-option
            v-for="ID in user_ID"
            :key="ID.userId"
            :value="ID.userId"
            :label="ID.userName"
          ></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="生产厂长审批状态" prop="approvalStatusProduct">-->
<!--        <el-select v-model="queryParams.approvalStatusProduct" placeholder="请选择生产厂长审批状态" clearable size="small">-->
<!--          <el-option label="未审核" value="0" />-->
<!--          <el-option label="审核通过" value="1" />-->
<!--          <el-option label="不通过" value="2" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="技术厂长审批状态" prop="approvalStatusTechnology">-->
<!--        <el-select v-model="queryParams.approvalStatusTechnology" placeholder="请选择技术厂长审批状态" clearable size="small">-->
<!--          <el-option label="未审核" value="0" />-->
<!--          <el-option label="审核通过" value="1" />-->
<!--          <el-option label="不通过" value="2" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="领用状态" prop="consumeStatus">
        <el-select v-model="queryParams.consumeStatus" placeholder="请选择领用状态" clearable size="small">
          <el-option label="未领用" value="0" />
          <el-option label="已领用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['fertilizer:applyTemporary:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['fertilizer:applyTemporary:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fertilizer:applyTemporary:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['fertilizer:applyTemporary:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyTemporaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" type="index" />
      <el-table-column label="肥料类型" align="center" prop="fertilizerType" />
      <el-table-column label="肥料名称" align="center" prop="fertilizerName" />
      <el-table-column label="NPK含量" align="center" prop="npkContent" />
      <el-table-column label="肥料品牌" align="center" prop="brand" />
      <el-table-column label="购自公司" align="center" prop="originCompany" />
      <el-table-column label="申请量" align="center" prop="applyAmount" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="100px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="userName" />
      <el-table-column label="生产厂长审批状态" align="center" prop="approvalStatusProduct" width="130">
        <template slot-scope="scope">
          <span>
            {{ approvalStatus[scope.row.approvalStatusProduct] }}
          </span>
          <div style="display: flex">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleThroughProduct(scope.row)"
              v-hasPermi="['fertilizer:applyTemporary:permit1']"
            >通过</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              @click="handleNotThroughProduct(scope.row)"
              v-hasPermi="['fertilizer:applyTemporary:permit1']"
            >不通过</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="技术厂长审批状态" align="center" prop="approvalStatusTechnology" width="130">
        <template slot-scope="scope">
          <span>
            {{ approvalStatus[scope.row.approvalStatusTechnology] }}
          </span>
          <div style="display: flex">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleThroughTechno(scope.row)"
              v-hasPermi="['fertilizer:applyTemporary:permit2']"
            >通过</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              @click="handleNotThroughTechno(scope.row)"
              v-hasPermi="['fertilizer:applyTemporary:permit2']"
            >不通过</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="领用状态" align="center" prop="consumeStatus" >
        <template slot-scope="scope">
          <span>
            {{consumeStatusList[scope.row.consumeStatus]}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['fertilizer:applyTemporary:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fertilizer:applyTemporary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改化肥临时申请对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
<!--      <el-form ref="form" :model="form" :rules="rules" label-width="100px">-->
<!--        <el-form-item label="类型" prop="fertilizerTypeId">-->
<!--          <el-select v-model="form.fertilizerTypeId" placeholder="请选择化肥类型">-->
<!--            <el-option-->
<!--              v-for="type in fertilizer_type"-->
<!--              :key="type.fertilizerTypeId"-->
<!--              :value="type.fertilizerTypeId"-->
<!--              :label="type.fertilizerType"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="名称" prop="fertilizerNameId">-->
<!--          <el-select v-model="form.fertilizerNameId" placeholder="请选择化肥名称">-->
<!--            <el-option-->
<!--              v-for="name in fertilizer_name"-->
<!--              :key="name.fertilizerNameId"-->
<!--              :value="name.fertilizerNameId"-->
<!--              :label="name.fertilizerName"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="NPK含量">-->
<!--          <el-input v-model="form.npkContent" type="textarea" placeholder="请输入内容"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="品牌" prop="brand">-->
<!--          <el-input v-model="form.brand" placeholder="请输入肥料品牌" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="公司" prop="originCompany">-->
<!--          <el-input v-model="form.originCompany" placeholder="请输入购自公司" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="申请量" prop="applyAmount">-->
<!--          <el-input v-model.number="form.applyAmount" placeholder="请输入申请量" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="申请时间" prop="applyTime">-->
<!--          <el-date-picker clearable size="small"-->
<!--            v-model="form.applyTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择申请时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import { listApplyTemporary, getApplyTemporary, delApplyTemporary, addApplyTemporary, updateApplyTemporary, exportApplyTemporary } from "@/api/production/fertilizer/applyTemporary";
import { listFertilizerType } from "@/api/production/fertilizer/fertilizerType";
import { listFertilizerName } from "@/api/production/fertilizer/fertilizerName";
import { listUser} from "@/api/system/user";

export default {
  name: "ApplyTemporary",
  components: {
  },
  data() {
    return {
      user_ID:[],
      //审核状态转换
      approvalStatus:{
        0:'未审核',
        1:'审核通过',
        2:'不通过'
      },
      consumeStatusList:{
        0:'未领用',
        1:'已领用'
      },
      //化肥类型
      fertilizer_type: [],
      //化肥名称
      fertilizer_name: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 化肥临时申请表格数据
      applyTemporaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fertilizerTypeId: null,
        fertilizerNameId: null,
        npkContent: null,
        brand: null,
        originCompany: null,
        applyAmount: null,
        applyTime: null,
        userId: null,
        approvalStatusProduct: null,
        approvalStatusTechnology: null,
        consumeStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fertilizerTypeId: [
          { required: true, message: "肥料类型不能为空", trigger: "blur" }
        ],
        fertilizerNameId: [
          { required: true, message: "肥料名称不能为空", trigger: "blur" }
        ],
        applyAmount: [
          { required: true, message: "申请量不能为空", trigger: "blur" },
          { type: 'number',
              message: '请输入非负数',
              trigger: 'blur',
              transform (value) {
                if (!/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value)) {
                  return true
                }else{
                }
              }
            }
        ],
        applyTime: [
          { required: true, message: "申请时间不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "申请人不能为空", trigger: "blur" }
        ],
        approvalStatusProduct: [
          { required: true, message: "生产厂长审批状态不能为空", trigger: "blur" }
        ],
        approvalStatusTechnology: [
          { required: true, message: "技术厂长审批状态不能为空", trigger: "blur" }
        ],
        consumeStatus: [
          { required: true, message: "领用状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    Promise.all([
      this.getUserID(),
      this.getFertilizerTypes(),
      this.getFertilizerNames(),
    ]).then((res)=>{
      this.getList();
    })
  },

  methods: {
    /**获取用户名称*/
    formdata_userList(data){
      let type_map = new Map();
      this.user_ID.forEach(type =>{
        type_map.set(type.userId,type.userName)
      })
      data.forEach(type => {
        let user_name = type_map.get(type.userId)
        type.userName = user_name
      });
    },
    getUserID(){
      return new Promise((resolve,rejected)=> {
        listUser().then(response =>{
          this.user_ID = response.rows;
          resolve()
        });
      })
    },
    //生产厂长审核通过
    handleThroughProduct(row) {
      this.reset();
      const fertilizerApplyTemporaryId = row.fertilizerApplyTemporaryId || this.ids;
      let data = {};
      data.fertilizerApplyTemporaryId = fertilizerApplyTemporaryId;
      data.approvalStatusProduct = 1;
      this.$confirm('是否确认通过该化肥审核?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateApplyTemporary(data).then(response => {})
      }).then(() => {
        this.getList();
      }).catch(() => {});
    },
    //生产厂长审核不通过
    handleNotThroughProduct(row) {
      this.reset();
      const fertilizerApplyTemporaryId = row.fertilizerApplyTemporaryId || this.ids;
      let data = {};
      data.fertilizerApplyTemporaryId = fertilizerApplyTemporaryId;
      data.approvalStatusProduct = 2;
      this.$confirm('是否确认不通过该条化肥审核?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateApplyTemporary(data).then(response => {})
      }).then(() => {
        this.getList();
      }).catch(() => {});
    },
    //技术厂长审核通过
    handleThroughTechno(row) {
      this.reset();
      const fertilizerApplyTemporaryId = row.fertilizerApplyTemporaryId || this.ids;
      let data = {};
      data.fertilizerApplyTemporaryId = fertilizerApplyTemporaryId;
      data.approvalStatusTechnology = 1;
      this.$confirm('是否确认通过该化肥审核?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateApplyTemporary(data).then(response => {})
      }).then(() => {
        this.getList();
      }).catch(() => {});
    },
    //技术厂长不审核通过
    handleNotThroughTechno(row) {
      this.reset();
      const fertilizerApplyTemporaryId = row.fertilizerApplyTemporaryId || this.ids;
      let data = {};
      data.fertilizerApplyTemporaryId = fertilizerApplyTemporaryId;
      data.approvalStatusTechnology = 2;
      this.$confirm('是否确认不通过该条化肥审核?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateApplyTemporary(data).then(response => {})
      }).then(() => {
        this.getList();
      }).catch(() => {});
    },
    //格式化化肥类型
    formdata_type(data) {
      let fertilizer_type_map = new Map();
      this.fertilizer_type.forEach((type) => {
        fertilizer_type_map.set(type.fertilizerTypeId, type.fertilizerType);
      });
      data.forEach((type) => {
        let fertilizer_type_name = fertilizer_type_map.get(type.fertilizerTypeId);
        type.fertilizerType = fertilizer_type_name;
      });
    },
    //格式化化肥名称
    formdata_name(data) {
      let fertilizer_name_map = new Map();
      this.fertilizer_name.forEach((type) => {
        fertilizer_name_map.set(type.fertilizerNameId, type.fertilizerName);
      });
      data.forEach((type) => {
        let fertilizer_name_name = fertilizer_name_map.get(type.fertilizerNameId);
        type.fertilizerName = fertilizer_name_name;
      });
    },

    //查询基质种类
    getFertilizerTypes() {
      return new Promise((resolve,rejected)=> {
        listFertilizerType().then((response) => {
          this.fertilizer_type = response.rows;
          resolve()
        });
      })
    },
    //查询基质类型
    getFertilizerNames() {
      return new Promise((resolve,rejected)=> {
        listFertilizerName().then((response) => {
          this.fertilizer_name = response.rows;
          resolve()
        });
      })
    },
    /** 查询化肥临时申请列表 */
    getList() {
      this.loading = true;
      listApplyTemporary(this.queryParams).then(response => {
        this.applyTemporaryList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.formdata_type(this.applyTemporaryList);
        this.formdata_userList(this.applyTemporaryList);
        this.formdata_name(this.applyTemporaryList);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fertilizerApplyTemporaryId: null,
        fertilizerTypeId: null,
        fertilizerNameId: null,
        npkContent: null,
        brand: null,
        originCompany: null,
        applyAmount: null,
        applyTime: null,
        userId: null,
        approvalStatusProduct: null,
        approvalStatusTechnology: null,
        consumeStatus: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fertilizerApplyTemporaryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加化肥临时申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fertilizerApplyTemporaryId = row.fertilizerApplyTemporaryId || this.ids
      getApplyTemporary(fertilizerApplyTemporaryId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改化肥临时申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fertilizerApplyTemporaryId != null) {
            updateApplyTemporary(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.approvalStatusProduct=0;
            this.form.approvalStatusTechnology=0;
            addApplyTemporary(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fertilizerApplyTemporaryIds = row.fertilizerApplyTemporaryId || this.ids;
      this.$confirm('是否确认删除该化肥临时申请?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApplyTemporary(fertilizerApplyTemporaryIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有化肥临时申请数据项?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportApplyTemporary(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
